<template>
  <div class="wrap">
    <template class="d-flex align-items-center">
      <p><b>{{ $t('message.HoleCmd.holePosition') }}</b></p>
    </template>
    <template class="d-flex align-items-center">
      <query-list
        :tipMsg="$t('message.HoleCmd.punchFace')"
        v-model="params.punchFace"
        @removeItem="removeSelect"
        :active="params.queryListActiveMap.punchFace"
        @switchQuery="switchQuery('punchFace')"/>
    </template>
    <template>
      <div class="align-items-center">
        <query-list
          :tipMsg="tipMsg"
          v-model="params.holePoint"
          @removeItem="removeSelect"
          :active="params.queryListActiveMap.holePoint"
          @switchQuery="switchQuery('holePoint')">
          <template v-slot:imgContent>
            <img src="./image/createSketch.svg" width="14" height="14" @click="createSketch">
          </template>
        </query-list>
<!--        <img v-if="params.holePoint.length === 0" src="./image/createSketch.svg" width="16" height="16" @click="createSketch">-->
      </div>
    </template>
    <template class="d-flex align-items-center">
      <p><b>{{$t('message.HoleCmd.holeStyle')}}</b></p>
    </template>
    <template>
      <!--样式-->
      <div class="d-flex align-items-center">
        <label class="el-form-item__label">{{$t('message.HoleCmd.holeType')}}：</label>
        <el-select v-model.number="params.holeType" @change="changeHoleType()">
          <el-option
            v-for="item in holedOptions"
            :key="item.value"
            :label="item.text"
            :value="item.value"/>
        </el-select>
      </div>
      <!--标准label-->
      <!--标准-->
      <div class="d-flex align-items-center">
        <label class="el-form-item__label">{{$t('message.HoleCmd.standardType')}}：</label>
        <el-select v-model.number="params.standard" @change="changeStandardTypeSizeMate()">
          <el-option
            v-for="item in params.standardOptions"
            :key="item.value"
            :label="item.text"
            :value="item.value"/>
        </el-select>
      </div>
      <!--类型 标准为“GB”条件时-->
      <div class="d-flex align-items-center" v-if="!params.isCustom">
        <label class="el-form-item__label">{{$t('message.HoleCmd.type')}}：</label>
        <el-select v-model.number="params.standardType" @change="changeStandardTypeSizeMate()">
          <el-option
            v-for="item in params.standardTypeOptions"
            :key="item.index"
            :label="item.text"
            :value="item.value"/>
        </el-select>
      </div>
      <!--规格 同上-->
      <div class="d-flex align-items-center" v-if="!params.isCustom">
        <label class="el-form-item__label">{{$t('message.HoleCmd.size')}}：</label>
        <el-select v-model.number="params.size" @change="changeStandardTypeSizeMate()">
          <el-option
            v-for="item in params.sizeOptions"
            :key="item.index"
            :label="item.text"
            :value="item.value"/>
        </el-select>
      </div>
      <!--配合 类型为“螺钉间隙”条件时-->
      <div class="d-flex align-items-center" v-if="!params.isCustom && params.changeShowMap.mate">
        <label class="el-form-item__label">{{$t('message.HoleCmd.mateType')}}：</label>
        <el-select v-model.number="params.mate" @change="changeStandardTypeSizeMate()">
          <el-option
            v-for="item in mateTypeOptions"
            :key="item.index"
            :label="item.text"
            :value="item.value"/>
        </el-select>
      </div>
    </template>
    <!--孔尺寸-->
    <template class="d-flex align-items-center">
      <p><b>{{$t('message.HoleCmd.holeDimension')}}</b></p>
    </template>
    <template>
        <!--      直径1-->
      <div class="d-flex align-items-center" v-if="params.changeShowMap.diameter1 && !isThreadedHole">
        <label class="el-form-item__label">{{params.changeLabelMap.diameter1}}：</label>
        <base-input-number
                v-model="params.diameter1.variableValue"
                @focus="inputFocus('diameter1')"
                @change="changeCommon('diameter1')"
                :min="0"
                :disabled="!isAllowReset"/>
        <label class="el-form-item__label">mm</label>
      </div>
      <!--      深度1-->
      <div class="d-flex align-items-center" v-if="params.changeShowMap.depth1 && !isThreadedHole">
        <label class="el-form-item__label">{{params.changeLabelMap.depth1}}：</label>
        <base-input-number
                v-model="params.depth1.variableValue"
                @focus="inputFocus('depth1')"
                @change="changeCommon('depth1')"
                :min="0"
                :disabled="!isAllowReset"/>
        <label class="el-form-item__label">mm</label>
      </div>
      <!--      角度1-->
      <div class="d-flex align-items-center" v-if="params.changeShowMap.angle1 && !isThreadedHole">
        <label class="el-form-item__label">{{params.changeLabelMap.angle1}}：</label>
        <base-input-number
                v-model="params.angle1.variableValue"
                @focus="inputFocus('angle1')"
                @change="changeCommon('angle1')"
                :min="0"
                :disabled="!isAllowReset"/>
        <label class="el-form-item__label">°</label>
      </div>
      <!--直径-->
      <div class="d-flex align-items-center">
        <label class="el-form-item__label">{{$t('message.HoleCmd.diameter')}}：</label>
        <base-input-number
                v-model="params.diameter.variableValue"
                @focus="inputFocus('diameter')"
                @change="changeCommon('diameter')"
                :min="0"
                :disabled="!isAllowReset"/>
        <label class="el-form-item__label">mm</label>
      </div>
      <!--顶锥角-->
      <div class="d-flex align-items-center" v-if="params.destinationType === params.destinationTypes.setDepth">
        <el-checkbox v-model="params.endFlag" :label="$t('message.HoleCmd.endFlag')" @change="changeCommon()"/>
        <base-input-number v-model="params.endAngle.variableValue"
                               :min="0"
                               :max="360"
                               :disabled="!params.endFlag"
                               @focus="inputFocus('endAngle')"
                               @change="changeCommon('endAngle')"/>
        <label class="el-form-item__label">°</label>
      </div>
      <!--恢复默认值-->
<!--      <div v-if="!params.isCustom && isAllowReset">-->
<!--        <div class="dialog-row tw-justify-center mt-3" v-if="isResetDefaultValue && !isFavorite">-->
<!--          <el-button class="dialog-btn" size="mini" @click="resetSizeValue()">-->
<!--            <slot>{{$t('message.HoleCmd.restoreDefaultValue')}}</slot>-->
<!--          </el-button>-->
<!--        </div>-->
<!--      </div>-->
    </template>
    <!--终止条件-->
    <template class="d-flex align-items-center">
      <p><b>{{$t('message.HoleCmd.destinationType')}}</b></p>
    </template>
    <template>
      <!-- 螺旋深度-->
      <div v-if="!isAllowReset">
        <div class="d-flex align-items-center" v-if="isThreadedHole">
          <label class="el-form-item__label">{{$t('message.HoleCmd.threadEndType')}}：</label>
          <el-select v-model.number="params.threadEndType" @change="changeCommon()">
            <el-option
              v-for="item in params.threadEndTypeOptions"
              :key="item.index"
              :label="item.text"
              :value="item.value"/>
          </el-select>
        </div>
        <div v-if="params.threadEndType !== params.threadEndTypes.through || isCounterThreaded" class="d-flex align-items-center">
          <label class="el-form-item__label">{{$t('message.HoleCmd.trDeep')}}：</label>
          <base-input-number v-model="params.depth1.variableValue"
                             :min="0.1"
                             @focus="inputFocus('depth1')"
                             @change="changeCommon('depth1')"/>
          <label class="el-form-item__label">mm</label>
          <div v-if="!isCounterThreaded">
            <img v-if="isThreadUsed" src="./image/autoComputeDepth1.svg" width="16" height="16" alt="" @click="isAutoComputeDepth('autoComputeDepth1')">
            <img v-else src="./image/autoComputeDepth2.svg" width="16" height="16" alt="" @click="isAutoComputeDepth('autoComputeDepth1')">
          </div>
        </div>
      </div>
      <!--终止深度-->
      <div class="d-flex align-items-center">
        <label class="el-form-item__label">{{$t('message.HoleCmd.method')}}：</label>
        <el-select v-model.number="params.destinationType" @change="changeCommon()">
          <el-option
            v-for="item in destinationTypeOptions"
            :key="item.index"
            :label="item.text"
            :value="item.value"/>
        </el-select>
      </div>
      <div v-if="params.destinationType === params.destinationTypes.setDepth" class="d-flex align-items-center">
        <label class="el-form-item__label">{{$t('message.HoleCmd.depth')}}：</label>
        <base-input-number v-model="params.depth.variableValue"
                           :min="0.1"
                           @focus="inputFocus('depth')"
                           @change="changeCommon('depth')"/>
        <label class="el-form-item__label">mm</label>
        <template v-if="!isAllowReset">
          <img v-if="isDestinationUsed" src="./image/autoComputeDepth1.svg" width="16" height="16" @click="isAutoComputeDepth('autoComputeDepth')">
          <img v-else src="./image/autoComputeDepth2.svg" width="16" height="16" @click="isAutoComputeDepth('autoComputeDepth')">
        </template>
      </div>
      <!--延伸到面,6.1不支持-->
<!--      <div class="ml-2" v-if="params.destinationType === params.destinationTypes.toFace">-->
<!--        <dialog-query-list :list="params.toFaceRef"-->
<!--                           :description="$t('message.HoleCmd.endFace')"-->
<!--                           :active="params.queryListActiveMap.toFaceRef"-->
<!--                           @switchActive="switchActiveState('toFaceRef')"-->
<!--                           @highlight="highlight($event)"-->
<!--                           @removeHighlight="removeHighlight($event)"-->
<!--                           @removeItem="changeCommon()"-->
<!--                           @removeAllItem="changeCommon()"-->
<!--                           :tipMsg="$t('message.HoleCmd.selEndFace')">-->
<!--        </dialog-query-list>-->
<!--      </div>-->
<!--      &lt;!&ndash;恢复默认值&ndash;&gt;-->
<!--      <div class="dialog-row tw-justify-center mt-3" v-if="!isAllowReset && isResetDepthDefaultValue && !isFavorite">-->
<!--        <el-button class="dialog-btn" size="mini" @click="resetValue()">-->
<!--          <slot>{{ $t('message.HoleCmd.restoreDefaultValue') }}</slot>-->
<!--        </el-button>-->
<!--      </div>-->
    </template>
    <!--显示效果-->
    <template  v-if="!isAllowReset">
      <template class="d-flex align-items-center">
        <p><b>{{ $t('message.HoleCmd.showEffect') }}</b></p>
      </template>
      <div v-if="params.changeShowMap.isShow" class="d-flex align-items-center">
        <label class="el-form-item__label">{{$t('message.HoleCmd.show')}}：</label>
        <el-select v-model.number="params.displayType" @change="changeCommon()">
          <el-option
            v-for="item in showOptions"
            :key="item.index"
            :label="item.text"
            :value="item.value"/>
        </el-select>
      </div>
    </template>
    <div class="btn">
      <el-button
        type="primary"
        size="small"
        :disabled="isDisabled()"
        @click="confirm"
      >
        {{ $t('message.HoleCmd.confirm') }}
      </el-button>
      <el-button
        size="small"
        @click="close"
      >
        {{ $t('message.HoleCmd.close') }}
      </el-button>
    </div>
  </div>
</template>

<script>
import {holeAndCutCommand} from "../../../Command/HoleAndCutCommand";
import {holeManager} from "./HoleManager";
import {holeValue} from "./HoleTypeStore";
import DialogMixin from "../DialogMixin";
import {ElementType} from "../../../modeling/TypeDefine";

export default  {
  mixins: [DialogMixin],
  name: "CmdDlgOpenHole",
  data() {
    return {
      holedOptions: [
        {text: this.$t("message.HoleCmd.simple"), value: 0},
        //{ text: this.$t("message.HoleCmd.taperedHole"), value: 1 },
        {text: this.$t("message.HoleCmd.counterbore"), value: 2},
        //{ text: this.$t("message.HoleCmd.drilledCounter"), value: 3 },
        {text: this.$t("message.HoleCmd.counterSunk"), value: 4},
        //{ text: this.$t("message.HoleCmd.cylCounterbore"), value: 5 },
        //{ text: this.$t("message.HoleCmd.hexCounterbore"), value: 6 },
        {text: this.$t("message.HoleCmd.threadedHole"), value: 7},
        {text: this.$t("message.HoleCmd.counterThreaded"), value: 8},
      ],  // 孔类型列表
      standardOptions1: [
        {text: this.$t("message.HoleCmd.gb"), value: 9}
      ], // 螺纹列表
      standardOptions2: [
        {text: this.$t("message.HoleCmd.custom"), value: 8},
        {text: this.$t("message.HoleCmd.gb"), value: 9}
      ], // 螺纹列表
      mateTypeOptions: [
        {text: this.$t("message.HoleCmd.tightness"), value: 1},
        {text: this.$t("message.HoleCmd.normal2"), value: 2},
        {text: this.$t("message.HoleCmd.loose"), value: 3},
      ], // 配合列表
      showOptions: [
        {text: this.$t("message.HoleCmd.RemovalOfThread"), value: 1},
        {text: this.$t("message.HoleCmd.ignoreThread"), value: 2},
        {text: this.$t("message.HoleCmd.decorative"), value: 3},
      ], //显示列表, 用于有显示方式时

      destinationTypeOptions: [
        {text: this.$t("message.HoleCmd.through"), value: 0},
        {text: this.$t("message.HoleCmd.setDepth"), value: 1},
        //{ text: this.$t("message.HoleCmd.fromTo"), value: 2 },
        //{ text: this.$t("message.HoleCmd.toFace"), value: 3 },
        //{ text: this.$t("message.HoleCmd.toNextObj"), value: 4 }
      ], // 终止方式
      params: {
        cmdMode: "CmdOpenHole",
        punchFace: [],
        holePoint: [],
        toFaceRef: [], //成型到面
        holeType: 0, //孔类型
        standard: 8, //标准
        standardType: 1, //标准类型
        size: 1, // 大小
        diameter: {
          variableValue:25,
          variableName:null
        }, //直径
        queryListActiveMap: {
          punchFace: 1,
          holePoint: 0,
        },
        filterTypes: [ElementType.FACE],
        standardOptions: [
          // {text: this.$t("message.HoleCmd.metricCoarsePitch"), value: 1},
          // {text: this.$t("message.HoleCmd.metricFinePitch"), value: 2},
          // {text: this.$t("message.HoleCmd.whitworth"), value: 3},
          // {text: this.$t("message.HoleCmd.gas"), value: 4},
          // {text: this.$t("message.HoleCmd.ansiUnc"), value: 5},
          // {text: this.$t("message.HoleCmd.ansiUnf"), value: 6},
          // {text: this.$t("message.HoleCmd.ansiUnef"), value: 7},
          {text: this.$t("message.HoleCmd.custom"), value: 8},
          {text: this.$t("message.HoleCmd.gb"), value: 9}
        ], // 标准列表
        threadEndTypeOptions: [
          {text: this.$t("message.HoleCmd.setDepth"), value: 1}
        ], // 螺纹深度方式

        isCustom: true,
        updateEnabled: true,
        standardTypeOptions: [], // 标准类型列表
        standardValues: {
          isStandard: false,
          diameter: {
            variableValue:0,
            variableName:null
          },
          depth: {
            variableValue:0,
            variableName:null
          },
          angle1: {
            variableValue:0,
            variableName:null
          },
          depth1: {
            variableValue:0,
            variableName:null
          },
          diameter1: {
            variableValue:0,
            variableName:null
          },
          endAngle: {
            variableValue:118,
            variableName:null
          },
        }, // 标准类型，用于恢复默认
        // 终止方式及相关数据
        destinationTypes: {
          through: 0,
          setDepth: 1,
          //fromTo: 2,
          toFace: 3,  // 不支持
          //toNextObj: 4
        }, // 终止类型
        threadEndTypes: {
          through: 0,
          setDepth: 1,
        }, // 螺纹深度类型
        changeShowMap: {
          mate: false, // 配合
          isShow: false, //显示
        }, // 根据不同标准类型/孔类型，切换不同的UI
        changeLabelMap: {
          angle1: "",
          depth1: "",
          diameter1: "",
        }, // 标签栏名字，用于切换不同标签名
        sizeOptions: [], // 大小列表

        // 终止方式及相关数据
        threadEndType: 1,  // 螺纹孔深度类型 默认给定深度
        destinationType: 1, // 终止方式类型 默认给定深度
        depth: {
          variableValue:50,
          variableName:null
        },//整体深度
        endFlag: true, // 是否结束角度
        endAngle: {
          variableValue:118,
          variableName:null
        }, // 结束角度数值

        // 显示方式
        displayType: 1,

        // 根据类型变动的数据
        mate: 1,
        angle1: {
          variableValue:90,
          variableName:null
        },
        depth1: {
          variableValue:10,
          variableName:null
        },
        diameter1: {
          variableValue:50,
          variableName:null
        },

        // 记录终止条件默认值相关
        defaultValue1: 0, // 记录螺纹深度默认值
        defaultValue: 0, // 记录终止深度默认值
        restoreDefaultValue: false, // 终止深度不同于默认值，用于显示恢复默认值按钮，true表示显示恢复默认值
        restoreDefaultValue1: false, // 螺纹深度不同于默认值，用于显示恢复默认值按钮，true表示显示恢复默认值

        // 记录孔尺寸默认值相关（GB标准下）
        sizeDefaultDiameter: 0, // 记录 钻孔直径 默认值
        sizeDefaultDiameter1: 0, // 记录 沉头直径/埋头直径/螺纹直径/锥形直径 默认值
        sizeDefaultDepth1: 0,  // 记录 沉头深度 默认值
        sizeDefaultAngle1: 0,  // 记录 埋头角度 默认值

        restoreSizeDefaultDiameter: false, // 钻孔直径 不同于默认值，用于显示恢复默认值按钮，true表示显示恢复默认值
        restoreSizeDefaultDiameter1: false, // 沉头直径/埋头直径/螺纹直径/锥形直径 不同于默认值，用于显示恢复默认值按钮，true表示显示恢复默认值
        restoreSizeDefaultDepth1: false, // 沉头深度 不同于默认值，用于显示恢复默认值按钮，true表示显示恢复默认值
        restoreSizeDefaultAngle1: false, // 埋头角度 不同于默认值，用于显示恢复默认值按钮，true表示显示恢复默认值

        // 自动计算深度
        autoComputeDepth: true,  // 终止深度
        autoComputeDepth1: true,  // 螺纹深度
        pitch: 0,  // 内核返回的用于自动计算深度
      },


      isEdit: false,
      tipMsg: this.$t('message.HoleCmd.holePoint'),
      // enterSketch: {
      //   src: require('../../../util/pictures/createSketch.svg'),
      // },
      setDepthOptions: [
        {text: this.$t("message.HoleCmd.setDepth"), value: 1}
      ],
      setDepthAndThroughOptions: [
        {text: this.$t("message.HoleCmd.setDepth"), value: 1},
        {text: this.$t("message.HoleCmd.through"), value: 0},
      ],
      isThreadUsed: true,//螺旋深度自动计算是否可用
      isDestinationUsed: true,//终止深度自动计算是否可用
      originValue: 0,//输入值修改前的原始值
    }
  },

  methods: {
    isDisabled() {
      if (this.params.punchFace.length > 0 && this.params.holePoint.length > 0) {
        return false;
      } else {
        return true;
      }
    },
    confirm(){
      let config = this.getCommandParams(false);

      let cmd = {
        name: "createHole",
        config: config,
      };

      holeAndCutCommand.executeHoleCmd(cmd, modelObj => {
      });
    },
    close(){
      holeAndCutCommand.closeActiveCmd();
    },
    init() {
      holeAndCutCommand.setDragSelectEnabled(false);
      holeManager.init();
      holeAndCutCommand.restoreParams(retValue => {
        let faceEle = retValue['punchFace'];
        if (faceEle){
          this.params.punchFace.push(faceEle);
        }
        let holePoints = retValue['holePoints'];
        if (holePoints){
          this.params.holePoint = holePoints;
        }
      })
      // this.params.editFlag = true;
      // this.handleHighlightEvent();
      // this.handlePickEvent();
      // this.handleItemSelected();
      // this.handleFeatureSelected();
      // this.initDebouncer();
      // this.preSelect("holePoint", this.cmdQuery.getQueryDefinition('holePoint').category);
      // this.prePunchFace();
      // selectionManager.clearSelections();
      // this.getHoleFavorites();
      //
      // // 填充params
      // if (featureManager.featureInEdit) {
      //   this.params.updateEnabled = false;
      //   let initCmd = new CommandData(
      //     "initHole",
      //     featureManager.featureInEdit.id,
      //     null,
      //   );
      //   hubConnection.executeCommands([initCmd], modelObj => {
      //     ModelParser.parseModelData(modelObj);
      //     let modelObjData = DataParser.getData(modelObj)
      //     let initData = DataParser.getInitData(modelObjData)
      //     this.editUndoData = DataParser.getUndoDataArray(initData);
      //     let paramData = DataParser.getParamData(modelObjData);
      //     if (paramData) {
      //       // 填充框
      //       if (paramData.HoleFace) {
      //         let element = elementManager.getElement(paramData.HoleFace);
      //         let elementInfo = makeElementInfo(element);
      //         this.params.punchFace = [];
      //         this.params.punchFace.push(elementInfo);
      //       }
      //       //point-old
      //       if (paramData.HolePoint) {
      //         let elementInfo = makePntInfo(paramData.HolePoint);
      //         this.params.holePoint = [];
      //         this.commandState.pushInfo(elementInfo, 'holePoint');
      //       }
      //       // points-new
      //       if (paramData.HolePoints) {
      //         this.params.holePoint = [];
      //         paramData.HolePoints.forEach(holePoint => {
      //           let elementInfo = makePntInfo(holePoint);
      //           if (elementInfo.pointType === PointOnCurveType.Center) {
      //             elementInfo.name = elementInfo.name.substring(0, elementInfo.name.indexOf(i18n.getMessage('message.ElementType.de'))) + i18n.getMessage('message.ElementType._center');
      //           }
      //           this.commandState.pushInfo(elementInfo, 'holePoint');
      //         })
      //       }
      //       //points-sketch
      //       if (paramData.HolePrfId) {
      //         this.params.holePoint = [];
      //         let contourRef = elementManager.getElement(paramData.HolePrfId);
      //         let elementInfo = makeElementInfo(contourRef);
      //         this.commandState.pushInfo(elementInfo, 'holePoint');
      //       }
      //       // toFace
      //       if (paramData.ToFace !== 0) {
      //         let element = elementManager.getElement(paramData.ToFace);
      //         let elementInfo = makeElementInfo(element);
      //         this.params.toFaceRef = [];
      //         this.params.toFaceRef.push(elementInfo);
      //       }
      //
      //       this.params.holeType = paramData.HoleType;
      //       if (paramData.StandardType) {
      //         let standard = this.params.standardOptions.find(s => s.text === paramData.StandardType);
      //         this.params.standard = standard.value;
      //         this.params.isCustom = this.params.standard === 8;
      //       }
      //       this.params.standardType = paramData.HoleForm === 0 ? 1 : paramData.HoleForm;
      //       holeManager.setHoleByHoleType(this.params.holeType);
      //       this.updateShowElements();
      //       this.params.size = this.getValueByText(this.params.sizeOptions, paramData.Designation);
      //       this.params.destinationType = paramData.EndCondition;
      //       if (this.params.destinationType === 0) {
      //         this.params.threadEndTypeOptions = setDepthAndThroughOptions;
      //       }
      //       this.params.displayType = paramData.DisplayType === 0 ? 1 : paramData.DisplayType;
      //       this.params.mate = paramData.CorType === 0 ? 1 : paramData.CorType;
      //       this.params.endFlag = paramData.HasEndAngle;
      //
      //       // 数值型参数
      //       if (paramData.Diameter?.VariableValue) {
      //         this.params.diameter = this.createVariable(paramData.Diameter.VariableValue, paramData.Diameter?.VariableName);
      //       }
      //       if (paramData.Depth?.VariableValue) {
      //         this.params.depth = this.createVariable(paramData.Depth.VariableValue, paramData.Depth?.VariableName);
      //       }
      //       // 内核返回的结束角度为正常值的一半
      //       if (paramData.Angle1?.VariableValue) {
      //         this.params.angle1 = this.createVariable(paramData.Angle1.VariableValue * 2, paramData.Angle1?.VariableName);
      //       }
      //       if (paramData.Depth1?.VariableValue) {
      //         this.params.depth1 = this.createVariable(paramData.Depth1.VariableValue, paramData.Depth1?.VariableName);
      //       }
      //       if (paramData.Diameter1?.VariableValue) {
      //         this.params.diameter1 = this.createVariable(paramData.Diameter1.VariableValue, paramData.Diameter1?.VariableName);
      //       }
      //       // 内核返回的结束角度为正常值的一半
      //       if (paramData.EndAngle?.VariableValue) {
      //         this.params.endAngle = this.createVariable(paramData.EndAngle.VariableValue * 2, paramData.EndAngle?.VariableName);
      //       }
      //
      //       // 标准化参数信息填充
      //       if (paramData.IsStandard && !this.params.standardValues.isStandard) {
      //         this.params.standardValues.isStandard = true;
      //         this.params.standardValues.diameter = this.createVariable(paramData.SDiameter);
      //         this.params.standardValues.depth = this.createVariable(paramData.SDepth);
      //         this.params.standardValues.angle1 = this.createVariable(paramData.SAngle1);
      //         this.params.standardValues.depth1 = this.createVariable(paramData.SDepth1);
      //         this.params.standardValues.diameter1 = this.createVariable(paramData.SDiameter1);
      //       }
      //       if ([0, 1].includes(paramData.ThreadEndType)) {
      //         this.params.threadEndType = paramData.ThreadEndType;
      //       }
      //       // 自动计算深度图标状态
      //       if (paramData.AutoComputeDepth === 0) {
      //         this.params.autoComputeDepth = false;
      //         this.params.autoComputeDepth1 = false;
      //       } else if (paramData.AutoComputeDepth === 1) {
      //         this.params.autoComputeDepth = false;
      //         this.params.autoComputeDepth1 = true;
      //       } else if (paramData.AutoComputeDepth === 2) {
      //         this.params.autoComputeDepth = true;
      //         this.params.autoComputeDepth1 = false;
      //       } else if (paramData.AutoComputeDepth === 3){
      //         this.params.autoComputeDepth = true;
      //         this.params.autoComputeDepth1 = true;
      //       }
      //       // holeFavorite
      //       if (paramData.HoleCustomStyle) {
      //         this.getFavoriteListByType();
      //         let initHoleFavorite = this.params.holeFavoriteList.find(info => info.id === paramData.HoleCustomStyle);
      //         this.params.selectedHoleFavorite = initHoleFavorite ? initHoleFavorite.value : this.params.selectedHoleFavorite;
      //       }
      //     }
      //
      //     // isCustom
      //     this.params.isCustom = this.params.standard === 8;
      //     this.initParams = JSON.parse(JSON.stringify(this.params));
      //     this.debouncer.next();
      //     this.updateHoleParams();  // 获取默认值
      //   });
      // } else {
        holeManager.setHoleByHoleType(this.params.holeType);
        this.updateShowElements();
        this.updateHoleParams();
        // paramsManager.restoreParams(this.cmdMode, this.params);
      // }
      // let radius1DragHandle = HandleFactory.createCommandHandle(HandleType.DRAG);
      // let radius2DragHandle = HandleFactory.createCommandHandle(HandleType.DRAG);
      // let deepDragHandle = HandleFactory.createCommandHandle(HandleType.DRAG);
      // let deep1DragHandle = HandleFactory.createCommandHandle(HandleType.DRAG);
      // let headAngleHandle = HandleFactory.createCommandHandle(HandleType.REVOLVE);
      // let endAngleHandle = HandleFactory.createCommandHandle(HandleType.REVOLVE);
      // radius1DragHandle.setBoundary(this.params.boundary.diameter);
      // radius2DragHandle.setBoundary(this.params.boundary.diameter);
      // deepDragHandle.setBoundary(this.params.boundary.depth);
      // deep1DragHandle.setBoundary(this.params.boundary.depth);
      // headAngleHandle.setBoundary(this.params.boundary.angle1);
      // endAngleHandle.setBoundary(this.params.boundary.endAngle);
      // radius1DragHandle.setReverseFlag(false);
      // radius2DragHandle.setReverseFlag(false);
      // deepDragHandle.setReverseFlag(false);
      // deep1DragHandle.setReverseFlag(false);
      // handleManager.setHandle("Diam", radius1DragHandle);
      // handleManager.setHandle("Diam1", radius2DragHandle);
      // handleManager.setHandle("DeepDragHandle", deepDragHandle);
      // handleManager.setHandle("Deep1DragHandle", deep1DragHandle);
      // handleManager.setHandle("Angle1", headAngleHandle);
      // handleManager.setHandle("EndAngleHandle", endAngleHandle);
      // handleManager.registerHandleEvent(this.onDrag);

      // eventBus.on('updateHoleFavorite', (holeFavoriteId) => {
      //   this.getFavoriteListByType();
      //   this.params.selectedHoleFavorite = this.params.holeFavoriteList.find(info => info.id === holeFavoriteId).value;
      // });
    },

    removeSelect(item) {
      if (item) {
        holeAndCutCommand.clearHighLightElements([item.id]);
      }
    },
    switchQuery(name) {
      for (const key in this.params.queryListActiveMap) {
        if (key === name){
          this.params.queryListActiveMap[key] = 1;
          let ids = this.params[key].map(ele => ele.id);
          if (ids) {
            holeAndCutCommand.highLightElements(ids);
          }
        }else{
          this.params.queryListActiveMap[key] = 0;
        }
      }
      if (name === "punchFace") {
        this.params.filterTypes = [ElementType.FACE];
      } else {
        this.params.filterTypes = [ElementType.POINT,ElementType.VERTEX,ElementType.ORIGINPOINT,ElementType.POINTONCURVE,ElementType.DATUMPOINT,ElementType.MASSREFPOINT,ElementType.SKETCHREGION];
      }
      holeAndCutCommand.setSelectType(this.params.filterTypes);
    },
    createSketch() {
      // preCmdManager.setPreCmdName("CmdHole", this.params);
      // if (this.params.punchFace.length > 0) {
      //   let holeFace = elementManager.getElement(this.params.punchFace[0].id);
      //   preCmdManager.setSketchRefPlane(this.params.punchFace[0]);
      //   sketchService.newSketch(holeFace, holeFace.type);
      // }
      // let tool = toolbarController.getTool("CmdPoint");
      // toolbarController.handleClick(tool);
      holeAndCutCommand.createSketchPoint(this.params);
    },
    changeHoleType() {
      if (!this.isAllowReset) {
        this.params.standard = 9;
        this.params.isCustom = false;
        this.params.standardOptions = this.standardOptions1;
      } else {
        this.params.standardOptions = this.standardOptions2
      }
      holeManager.setHoleByHoleType(this.params.holeType);
      this.updateShowElements();
      this.updateHoleParams();
    },
    // 更新显示的UI，处理暂存的数据
    updateShowElements() {
      this.params.standardValues.isStandard = false;

      // 更新前端UI面板上填充的各类数据信息
      if (holeManager.isChangeHoleType(this.params.holeType)) {
        this.updateStandTypeOptions();
      }
      if (!this.params.isCustom) {
        if (holeManager.isChangeHoleType(this.params.holeType)) {
          this.updateStandTypeOptions();
        }
      } else {
        this.resetShowOptions();
      }
      this.updateSizeOptions();
      this.updateChangeShowMap();
    },
    // 更新标准列表数据参数 -- 以下属于更新前端UI时获取参数的方法，holeManager与相关的孔数据仓库
    updateStandTypeOptions() {
      this.params.standardTypeOptions = holeManager.getStandardOptionsByValue(this.params.holeType, this.params.standard) || 1;
      if (this.params.standardType > this.params.standardTypeOptions.length) {
        this.params.standardType = 1;
      }
    },
    // 更新规格大小列表数据
    updateSizeOptions() {
      this.params.sizeOptions = holeManager.getHoleStandardSizeOption(this.params.standardType, this.params.holeType, this.params.standard) || 1;
      if (this.params.size > this.params.sizeOptions.length) {
        this.params.size = 1;
      }
    },
    // 更新变动的参数信息————对应holeStandard-showTypes
    updateChangeShowMap() {
      let types = holeManager.getShowTypes(this.params.standardType);
      let showTypes = [...types];
      for (let changeShowMapKey in this.params.changeShowMap) {
        this.params.changeShowMap[changeShowMapKey] = showTypes.includes(changeShowMapKey);
      }
      this.updateChangeLabel()
    },
    // 更新变动的参数信息————对应holeStandard-labelShowTypes
    updateChangeLabel() {
      let labelTypes = holeManager.getLabelTypeObj();
      for (let labelTypesKey in labelTypes) {
        if (labelTypes[labelTypesKey]) {
          let msg = 'message.HoleCmd.' + labelTypes[labelTypesKey];
          this.params.changeLabelMap[labelTypesKey] = this.$t(msg);
          this.params.changeShowMap[labelTypesKey] = true;
        } else {
          this.params.changeLabelMap[labelTypesKey] = "";
          this.params.changeShowMap[labelTypesKey] = false;
        }
      }
    },
    // 重置UI参数信息
    resetShowOptions() {
      this.params.standardTypeOptions = [];
      this.params.sizeOptions = [];
      this.updateChangeShowMap();
      this.params.changeShowMap.isShow = [holeValue.THREADED_HOLE, holeValue.COUNTER_THREADED].includes(this.params.holeType);
    },
    // 获取文字
    getTextByValue(list, value) {
      if (this.params.isCustom) {
        return ""
      }
      let standard = list.find(standard => standard.value === value);
      return standard?.text || "";
    },
    // 解析直径符号（脚本发送）
    getNewSize() {
      let size = this.getTextByValue(this.params.sizeOptions, this.params.size);
      return size.replace('Ø', 'FDIA');
    },
    getCommandParams(isPreview = true) {
      let idName = 'id';
      if (!isPreview) {
        idName = 'topoId';
      }
      let holeFace = this.params.punchFace[0].id;
      let holePoints = [];
      let holeType = this.params.holeType;
      let endCondition = this.params.destinationType;
      let isStandard = this.params.isCustom ? 0 : 1;
      let standardType = this.getTextByValue(this.params.standardOptions, this.params.standard);
      let designation = this.getNewSize();
      let hasEndAngle = this.params.endFlag && this.params.destinationType === this.params.destinationTypes.setDepth ? 1 : 0;
      let endAngle = this.params.endAngle;
      let depth = this.params.depth;
      let diameter = this.params.diameter;
      let angle1 = this.params.angle1;
      let depth1 = this.params.depth1;
      let diameter1 = this.params.diameter1;
      let toFace = idName === 'id' ? 0 : "";
      let holeForm = this.params.standardType;
      let coordinateType = this.params.mate;
      let displayType = this.params.displayType;
      let holePrf = idName === 'id' ? 0 : "";
      let threadEndType = this.params.threadEndType;
      let autoComputeDepth;
      // 保存自动计算深度按钮情况
      if (!this.params.autoComputeDepth && !this.params.autoComputeDepth1) {
        autoComputeDepth = 0;
      } else if (!this.params.autoComputeDepth && this.params.autoComputeDepth1) {
        autoComputeDepth = 1;
      } else if (this.params.autoComputeDepth && !this.params.autoComputeDepth1) {
        autoComputeDepth = 2;
      } else {
        autoComputeDepth = 3;
      }
      // if (this.params.holePoint[0]?.type === EntityType.SKETCH) {
      //   holePrf = entityManager.getEntity(this.params.holePoint[0].id)[idName];
      // } else {
        this.params.holePoint.forEach(holePnt => {
          holePoints.push(holePnt);
        });
      // }
      if (this.params.destinationType === this.params.destinationTypes.toFace) {
        if (this.params.toFaceRef.length > 0) {
          toFace = this.params.toFaceRef[0].id;
        }
      }
      // let favoriteId = this.params.selectedHoleFavorite > 0 ? this.params.holeFavoriteList[this.params.selectedHoleFavorite].id : '';

      return {
        holeFace,
        holePoints,
        holeType,
        endCondition,
        isStandard,
        standardType,
        designation,
        hasEndAngle,
        endAngle,
        depth,
        diameter,
        angle1,
        depth1,
        diameter1,
        toFace,
        holeForm,
        coordinateType,
        displayType,
        holePrf,
        threadEndType,
        autoComputeDepth,
        // favoriteId,
      }
    },
    //!<预览
    preview() {
      //清除预览数据
      holeAndCutCommand.clearPreview();
      if (this.params.punchFace.length <= 0 || this.params.holePoint.length <= 0){
        return;
      }
      let config = this.getCommandParams();
      let cmd = {
        name: "previewHole",
        config: config,
      };

      holeAndCutCommand.executeHoleCmd(cmd, paramData => {
        // ModelParser.parseModelData(modelObj);
        // this.commandState.previewError = DataParser.getStatus(modelObj) !== 0;
        // if (this.commandState.previewError) {
        //   this.clearPreview();
        //   return;
        // }
        // let modelObjData = DataParser.getData(modelObj);
        // let paramData = DataParser.getParamData(modelObjData);
        // if (modelObjData?.HandleData) {
        //   let handleData = DataParser.getHandleDataObjArray(modelObjData);
        //   this.initHandle(handleData);
        // }
        // if (this.params.editFlag) {
        //   this.params.standardValues.isStandard = true;
        //   this.params.editFlag = false;
        // }
        if (!this.params.isCustom) {
          // 数值型参数
          this.params.diameter = this.createVariable(paramData.Diameter !== 0 ? paramData.Diameter : 10);
          this.params.depth = this.createVariable(paramData.Depth !== 0 ? paramData.Depth : 10);
          this.params.angle1 = this.createVariable(paramData.Angle1 !== 0 ? paramData.Angle1 * 2 : 10);
          this.params.depth1 = this.createVariable(paramData.Depth1 !== 0 ? paramData.Depth1 : 10);
          this.params.diameter1 = this.createVariable(paramData.Diameter1 !== 0 ? paramData.Diameter1 : 10);
          if (!this.params.standardValues.isStandard && !this.params.isCustom) {
            this.params.standardValues.isStandard = true;
            this.params.standardValues.diameter = this.createVariable(paramData.Diameter);
            this.params.standardValues.depth = this.createVariable(paramData.Depth);
            this.params.standardValues.angle1 = this.createVariable(paramData.Angle1);
            this.params.standardValues.depth1 = this.createVariable(paramData.Depth1);
            this.params.standardValues.diameter1 = this.createVariable(paramData.Diameter1);
          }
        }
        this.params.updateEnabled = true;
      });
    },
    createVariable(value){
      let varObject = {
        variableValue:value,
        variableName:null
      };
      return varObject;
    },
    // 更新其他参数发送脚本
    updateHoleParams() {
      let cmd;
      let standardType = this.getTextByValue(this.params.standardOptions, this.params.standard);
      let designation = this.getNewSize();
      let config = {
        holeType: this.params.holeType,
        isStandard: this.params.isCustom ? 0 : 1,
        standardType: standardType,
        designation: designation,
        holeForm: this.params.standardType,
        coordinateType: this.params.mate,
        displayType: this.params.displayType,
      };
      cmd = {
        name: "getHoleParams",
        config: config,
      };
      holeAndCutCommand.executeHoleCmd(cmd, (paramData) => {
        if (paramData) {
          if (this.params.updateEnabled) {
            if (paramData.Diameter) {
              if (!this.params.diameter) {
                this.params.diameter = this.createVariable(paramData.Diameter);
              } else {
                this.params.diameter.variableValue = paramData.Diameter;
              }
            }
            if (paramData.Depth) {
              if (!this.params.depth) {
                this.params.depth = this.createVariable(paramData.Depth);
              } else {
                this.params.depth.variableValue = paramData.Depth;
              }
            }
            if (paramData.Angle1) {
              if (!this.params.angle1) {
                this.params.angle1 = this.createVariable(paramData.Angle1);
              } else {
                this.params.angle1.variableValue = paramData.Angle1;
              }
            }
            if (paramData.Depth1) {
              if (!this.params.depth1) {
                this.params.depth1 = this.createVariable(paramData.Depth1);
              } else {
                this.params.depth1.variableValue = paramData.Depth1;
              }
            }
            if (paramData.Diameter1) {
              if (!this.params.diameter1) {
                this.params.diameter1 = this.createVariable(paramData.Diameter1);
              } else {
                this.params.diameter1.variableValue = paramData.Diameter1;
              }
            }
          }
          if (paramData.Pitch) {
            this.params.pitch = paramData.Pitch;
          }

          // 螺纹孔与锥形管螺纹的 终止深度项组 默认值获取及非默认值效果
          if (!this.isAllowReset) {
            this.params.defaultValue = paramData.Depth;
            this.params.restoreDefaultValue = this.params.defaultValue !== this.params.depth;
            this.params.defaultValue1 = paramData.Depth1;
            this.params.restoreDefaultValue1 = this.params.defaultValue1 !== this.params.depth1;
          }
          if (this.params.destinationType === 0) {
            this.params.restoreDefaultValue = false;
          }
          if (this.params.threadEndType === 0) {
            this.params.restoreDefaultValue1 = false;
          }
          if (this.params.standard === 9) {
            // 孔尺寸默认值获取，顶锥角默认值由前端设置
            this.params.sizeDefaultDiameter = paramData.Diameter;
            this.params.sizeDefaultDiameter1 = paramData.Diameter1;
            this.params.sizeDefaultDepth1 = paramData.Depth1;
            this.params.sizeDefaultAngle1 = paramData.Angle1;

            this.params.restoreSizeDefaultDiameter = parseFloat(this.params.sizeDefaultDiameter.toFixed(3)) !== parseFloat(this.params.diameter.variableValue.toFixed(3));
            this.params.restoreSizeDefaultDiameter1 = parseFloat(this.params.sizeDefaultDiameter1.toFixed(3)) !== parseFloat(this.params.diameter1.variableValue.toFixed(3));
            this.params.restoreSizeDefaultDepth1 = parseFloat(this.params.sizeDefaultDepth1.toFixed(3)) !== parseFloat(this.params.depth1.variableValue.toFixed(3));
            this.params.restoreSizeDefaultAngle1 = parseFloat(this.params.sizeDefaultAngle1.toFixed(3)) !== parseFloat(this.params.angle1.variableValue.toFixed(3));
          }
        }
        this.preview();
      });
    },
    changeStandardTypeSizeMate() {
      this.params.isCustom = this.params.standard === 8;
      this.updateShowElements();
      this.updateHoleParams();
    },
    isAllowReset() {
      return ![holeValue.THREADED_HOLE, holeValue.COUNTER_THREADED].includes(this.params.holeType);
    },
    isAutoComputeDepth(isAutoComputeDepth) {
      if (isAutoComputeDepth === 'autoComputeDepth1') {
        this.isThreadUsed = !this.isThreadUsed;
        if (this.isThreadUsed) {
          return;
        }
      }else{
        this.isDestinationUsed = !this.isDestinationUsed;
        if (this.isDestinationUsed){
          return;
        }
      }

      let x;   // 计算常量
      if (this.params.holeType === 7) {
        if (this.params.standardType === 1 || this.params.standardType === 3) {
          x = this.params.pitch * 3;
        } else if (this.params.standardType === 2) {
          x = this.params.pitch * 5;
        }
      } else if (this.params.holeType === 8) {
        if (this.params.standardType === 1) {
          x = this.params.pitch * 5;
        }
      }
      if (isAutoComputeDepth === 'autoComputeDepth1') {
        this.autoComputeDepthALogic(x);
      } else {
        this.autoComputeDepthBLogic(x);
      }
    },
    // 螺纹孔自动计算深度
    autoComputeDepthALogic(x) {
      if (x === 0 || !this.params.depth) {
        return;
      }
      let diffenerces = this.params.depth.variableValue - x;
      // 自动计算的值不符合正常范围时，停止自动计算
      if (diffenerces < 0.001) {
        return;
      }
      if (diffenerces <= this.params.depth1.variableValue) {
        this.params.depth1.variableValue = diffenerces;
        // this.params.restoreDefaultValue1 = parseFloat(this.params.defaultValue1.toFixed(userOptionManager.measureUnit.lengthPrecision)) !== parseFloat(this.params.depth1.variableValue.toFixed(userOptionManager.measureUnit.lengthPrecision));
      }
      // this.debouncer.next();
    },

    // 终止深度自动计算深度
    autoComputeDepthBLogic(x) {
      if (x === 0 || !this.params.depth1.variableValue) {
        return;
      }
      this.params.depth.variableValue = this.params.depth1.variableValue + x;
      // this.params.restoreDefaultValue = parseFloat(this.params.defaultValue.toFixed(userOptionManager.measureUnit.lengthPrecision)) !== parseFloat(this.params.depth.variableValue.toFixed(userOptionManager.measureUnit.lengthPrecision));
      // this.debouncer.next();
    },
    inputFocus(name) {
      this.originValue = this.params[name].variableValue;
    },
    changeCommon(name) {
      // if (value === 'depth' && this.params.autoComputeDepth) {
      //   this.changeImgList('autoComputeDepth');
      // } else if (value === 'depth1' && this.params.autoComputeDepth1) {
      //   this.changeImgList('autoComputeDepth1');
      // }
      // if (value <= 0.0001) {
      //   this.params.diameter1.variableValue = this.params.diameter1.variableValue;
      // }
      if (name) {
        let value = this.params[name].variableValue;
        if (value <= 0) {
          this.params[name].variableValue = this.originValue;
        }
      }
      if (!this.isAllowReset) {
        // 自动计算螺纹孔螺纹深度
        if (this.params.holeType === 7 && this.params.autoComputeDepth1) {
          this.isAutoComputeDepth('autoComputeDepth1');
        } else if (this.params.autoComputeDepth) {
          this.isAutoComputeDepth('autoComputeDepth');
        }
      }

      // 当终止深度为“贯穿全部”时，更新螺纹深度类型
      if (this.params.destinationType === 0) {
        this.params.threadEndTypeOptions = this.setDepthAndThroughOptions;
      } else if (this.params.destinationType === 1) {
        this.params.threadEndType = 1;
        this.params.threadEndTypeOptions = this.setDepthOptions;
      }

      this.preview();
    },
    changePluginContainerHeight() {
      holeAndCutCommand.changePluginContainerHeight({minWidth: this.$el.clientWidth, maxHeight: this.$el.clientHeight})
    }
  },

  mounted(){
    holeAndCutCommand.init(500, 250, 500, () => {
      this.init();
      holeAndCutCommand.setSelectType(this.params.filterTypes);
      holeAndCutCommand.handlePickElement((objects) => {
        if (objects.length === 0) return;
        objects.forEach(object => {
          if (this.params.queryListActiveMap.punchFace === 1) {
            if (object.type === 19) {//ElementType.FACE
              let flag = this.params.punchFace.some((ele) => object.id === ele.id);
              if (flag) {
                return;
              }
              this.params.punchFace.splice(0);
              this.params.punchFace.push(object);
              this.switchQuery("holePoint");
            }
          }else {
            if ([ElementType.POINT,ElementType.VERTEX,ElementType.ORIGINPOINT,ElementType.POINTONCURVE,ElementType.DATUMPOINT,ElementType.MASSREFPOINT,ElementType.SKETCHREGION].includes(object.type)){
              let flag = this.params.holePoint.some((ele) => object.id === ele.id);
              if (flag) {
                return;
              }
              this.params.holePoint.push(object);
            }
          }
        })
      })
    });
  },
  computed: {
    isAllowReset() {
      return ![holeValue.THREADED_HOLE, holeValue.COUNTER_THREADED].includes(this.params.holeType);
    },
    isThreadedHole() {
      return this.params.holeType === holeValue.THREADED_HOLE;
    },
    isCounterThreaded() {
      return this.params.holeType === holeValue.COUNTER_THREADED;
    },
    isConfirm() {
      return !this.params.punchFace && this.params.punchFace.length > 0 && !this.params.holePoint && this.params.holePoint.length > 0
    }
  },

  watch: {
    "params.punchFace": {
      handler(elements) {
        let ids = elements.map(ele => ele.id);
        if (ids && ids.length > 0) {
          holeAndCutCommand.highLightElements(ids);
        }
        this.preview();
      }
    },
    "params.holePoint": {
      handler(elements) {
        let ids = elements.map(ele => ele.id);
        if (ids) {
          holeAndCutCommand.highLightElements(ids);
        }
        this.preview();
      }
    },
    // "params.holeType": function () {
    //   this.changeHoleType();
    // },
    // "params.standard":function (){
    //   this.changeStandardTypeSizeMate();
    // },
    // "params.standardType": function (){
    //   this.changeStandardTypeSizeMate();
    // },
    // "params.size":function (){
    //   this.changeStandardTypeSizeMate();
    // },
    // "params.mate":function () {
    //   this.changeStandardTypeSizeMate();
    // },
    // "params.destinationType":function (){
    //   this.changeCommon();
    // },
  },
}
</script>

<style scoped>
.wrap {
  font-size: 12px;
}

label {
  color: var(--text-color1);
}

.content {
  margin-bottom: 7px;
  width: 100%;
  display: flex;
  align-items: center;
}

.container{
  display: flex;
}
.box {
  flex: 100%;
}

</style>